package com.hashbee.bbs.service.impl;

import com.hashbee.bbs.bo.PageData;
import com.hashbee.bbs.bo.para.PageQueryPara;
import com.hashbee.bbs.dao.NotificationMapper;
import com.hashbee.bbs.dao.NotificationReadMapper;
import com.hashbee.bbs.domain.Notification;
import com.hashbee.bbs.domain.NotificationRead;
import com.hashbee.bbs.service.NotificationService;
import com.hashbee.bo.ReturnData;
import com.hashbee.user.model.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("notificationService")
public class NotificationServiceImpl implements NotificationService {

    @Autowired
    private NotificationMapper notificationMapper;

    @Autowired
    private NotificationReadMapper notificationReadMapper;

    @Override
    public ReturnData<PageData<Notification>> queryPage(PageQueryPara pageQueryPara) {

        ReturnData<PageData<Notification>> returnData = new ReturnData<>();
        UserVo user;
        try {
            user = (UserVo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {
            returnData.setStatus(1007);
            returnData.setMessage("未登录");
            return returnData;
        }

        pageQueryPara.setUserId(user.getId().toString());
        PageData<Notification> pageData = new PageData<>();
        pageData.setList(notificationMapper.selectPage(pageQueryPara));
        pageData.setCount(notificationMapper.selectCount(pageQueryPara));

        returnData.setData(pageData);

        return returnData;
    }

    @Override
    public ReturnData<Notification> queryDetail(PageQueryPara pageQueryPara) {
        ReturnData<Notification> returnData = new ReturnData<>();
        UserVo user;
        try {
            user = (UserVo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {
            returnData.setStatus(1007);
            returnData.setMessage("未登录");
            return returnData;
        }

        pageQueryPara.setUserId(user.getId().toString());
        returnData.setData(notificationMapper.selectDetail(pageQueryPara));

        // 标记为已读
        NotificationRead notificationRead = new NotificationRead();
        notificationRead.setNotificationId(pageQueryPara.getNotificationId());
        notificationRead.setTargetId(user.getId().toString());
        notificationRead.setTargetType("user");
        notificationReadMapper.insertDuplicate(notificationRead);

        return returnData;
    }

    @Override
    public ReturnData<Integer> read(String notificationIds) {
        ReturnData<Integer> returnData = new ReturnData<>();
        UserVo user;
        try {
            user = (UserVo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {
            returnData.setStatus(1007);
            returnData.setMessage("未登录");
            return returnData;
        }

        List<NotificationRead> notificationReadList = new ArrayList<>();
        String[] notificationIdArray = notificationIds.split(",");
        for (String notificationId : notificationIdArray) {
            NotificationRead notificationRead = new NotificationRead();
            notificationRead.setNotificationId(Integer.parseInt(notificationId));
            notificationRead.setTargetType("user");
            notificationRead.setTargetId(user.getId().toString());
            notificationReadList.add(notificationRead);
        }
        Map<String, Object> notificationReadListMap = new HashMap<>();
        notificationReadListMap.put("list", notificationReadList);
        notificationReadListMap.put("lastUpdateTime", new Date());
        notificationReadMapper.insertDuplicateList(notificationReadListMap);

        return returnData;
    }

    @Override
    public ReturnData<Integer> unread() {
        ReturnData<Integer> returnData = new ReturnData<>();
        UserVo user;
        try {
            user = (UserVo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {
            returnData.setStatus(1007);
            returnData.setMessage("未登录");
            return returnData;
        }

        Integer unreadCount = notificationReadMapper.selectUnreadCount(user.getId().toString());
        if (unreadCount == null) {
            unreadCount = 0;
        }
        returnData.setData(unreadCount);
        return returnData;
    }
}
